const tokenController = require('../controller/tokenController')

module.exports = async (req, res, next) => {
  const { headers } = req
  // eslint-disable-next-line
  if (!headers.hasOwnProperty('authorization')) {
    next()
  }
  const authorizationArray = (headers.authorization && headers.authorization.split(' ')) || null
  const token = authorizationArray[1]
  // 根据token的type不同采取不同的处理，主要分为shop user 和manager两大类
  const TokenType = authorizationArray[0]
  let uuId = null
  try {
    uuId = await tokenController.checkToken(token, TokenType)
    req.uuId = uuId
    req.type = TokenType
    next()
    return
  } catch (e) {
    next(e)
  }
}
